﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL.CommonDAL;
using MINERP.DAL;
using System.Transactions;
using MINERP.COM;
using System.IO;

namespace MINERP.BLL.LeaderOperate
{
    public class BackOrderBLL
    {
        private OrderInfoDAL _oidal;
        public BackOrderBLL()
        {
            _oidal = new OrderInfoDAL();
        }

        public OrderInfo_Info GetOrderinfoById(int oiId)
        {
            if (oiId < 0)
            {
                return null;
            }
            orderInfo oi = _oidal.GetOneById(oiId);
            return new OrderInfo_Info() { OrderProductCode = oi.OrderProductCode, CtmName = oi.CtmName, Remark = oi.Remark, OrderProductName =oi.OrderProductName };
        }

        /// <summary>
        /// 返工
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        /// <param name="p_3"></param>
        /// <returns>-1失败，0成功</returns>
        public int RebackWork(int orderProductId, int backTechId, string remark)
        {
            WorkOrderFlowList backWFL = null, currentWFL = null, designWFL = null;
            int result = 0;

            using (newSampleYPDBEntities data = new newSampleYPDBEntities())
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    try
                    {
                        //orderInfo oi = _oidal.GetOneById(orderProductId);
                        orderInfo oi = data.orderInfo.FirstOrDefault(o => o.OrderProductId == orderProductId);
                        if (oi==null)
                        {
                            return -4;
                        }
                        #region 加载数据
                        if (!oi.WorkOrderFlowList.IsLoaded)
                        {
                            oi.WorkOrderFlowList.Load();
                        }
                        if (!oi.photoInfo.IsLoaded)
                        {
                            oi.photoInfo.Load();
                        }
                        #endregion

                        #region 得到backWFL\currentWFL\designWFL
                        foreach (var wfl in oi.WorkOrderFlowList)
                        {
                            if (!wfl.tb_TechReference.IsLoaded)
                            {
                                wfl.tb_TechReference.Load();
                            }

                            if (wfl.tb_Tech.IsDesign == true)
                            {
                                designWFL = wfl;
                            }
                            if (wfl.tb_Tech.TechId == backTechId)
                            {
                                backWFL = wfl;
                                continue;
                            }
                            if (wfl.Status == (int)OrderFlowListStatus.Useable || wfl.Status == (int)OrderFlowListStatus.UseableAccept)
                            {
                                currentWFL = wfl;
                                continue;
                            }

                            if (backWFL != null && currentWFL != null)
                            {
                                break;
                            }
                        }
                        #endregion
                        if (currentWFL == null)
                        {
                            result = -3;//不能返工给还在操作的岗位
                        }
                        else
                        {
                            if (backWFL.WorkOrder >= currentWFL.WorkOrder)
                            {
                                result = -2;//不能返工到还未做的岗位
                            }
                            else
                            {
                                #region 修改workflowList状态，以及产品的备注
                                oi.Remark = remark;
                                backWFL.Status = (int)OrderFlowListStatus.Useable;
                                foreach (var wfl in oi.WorkOrderFlowList.Where(wl => wl.WorkOrder > backWFL.WorkOrder))
                                {
                                    wfl.Status = (int)OrderFlowListStatus.Wait;
                                    wfl.UserId = 0;
                                }
                                #endregion
                            }

                            #region 改变照片的路径
                            if (designWFL != null)
                            {
                                if (currentWFL.WorkOrder > designWFL.WorkOrder && backWFL.WorkOrder <= designWFL.WorkOrder)
                                {
                                    //1、标注原的照片为“设计册除”状态，即标注成这种状态的照片是设计的，需要在下次导入设计稿时被册除的
                                    //2、遍历所有上层目录的内的JPG图片，并将信息添加到数据库中
                                    photoInfo oldPi = oi.photoInfo.FirstOrDefault();
                                    FileInfo fi = new FileInfo(oldPi.picPath);
                                    DirectoryInfo di = fi.Directory.Parent;//父父目录

                                    foreach (var pi in oi.photoInfo)
                                    {
                                        pi.status = (int)PhotoStatus.DesignDelete;
                                    }

                                    foreach (var f in di.GetFiles("*.jpg|*.JPG|*.jpeg|*.tif|*.tiff"))
                                    {
                                        oi.photoInfo.Add(new photoInfo()
                                        {
                                            OldPicPath = f.FullName,
                                            paperFace = oldPi.paperFace,
                                            paperType = oldPi.paperType,
                                            PhotoBarCode = "xxx",
                                            picPath = f.FullName,
                                            photoName = f.Name,
                                            Remark = f.Name,
                                            status = oldPi.status,
                                        });

                                    }

                                    DALBase.Save();

                                    //更新照片的barCode
                                    oi.photoInfo.Load();
                                    foreach (var newPhoto in oi.photoInfo.Where(pi => pi.status == oldPi.status))
                                    {
                                        newPhoto.PhotoBarCode = string.Format("3{d:9}", newPhoto.photoId);
                                    }

                                    DALBase.Save();
                                }
                            }
                            #endregion
                        }
                        DALBase.Save();
                        ts.Complete();
                    }
                    catch (Exception)
                    {
                        result = -1;
                    }
                    return result;
                }
            }
        }
    }
}
